/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.model.opengl1;

import com.navid.trafalgar.model.opengl2.*;
import com.navid.trafalgar.model.GameModel;
import com.jme3.asset.AssetManager;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.util.SkyFactory;
import com.navid.trafalgar.definition.Entry;
import com.navid.trafalgar.definition.GameDefinition;
import com.navid.trafalgar.definition.MillestoneDefinition;
import com.navid.trafalgar.definition.ShipDefinition;
import com.navid.trafalgar.definition.TerrainDefinition;
import com.navid.trafalgar.definition.TextureDefinition;
import com.navid.trafalgar.model.ASimpleWater;
import com.navid.trafalgar.model.IContext;
import com.navid.trafalgar.model.IModelBuilder;
import com.navid.trafalgar.model.SimpleContext;
import com.navid.trafalgar.model.SunModel;

/**
 *
 * @author alberto
 */
public class ModelBuilder implements IModelBuilder {

    AssetManager assetManager;

    public ModelBuilder(AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    //TODO: redo
    public ShipModel buildShip(ShipDefinition shipDefinition) {

        ShipModel shipModel = new ShipModel(assetManager);

        Material mat_tt = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_tt.setTexture("ColorMap", assetManager.loadTexture("Materials/images.jpg"));
        mat_tt.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency

        shipModel.setMaterial(mat_tt);

        return shipModel;
    }

    public MillestoneModel buildMillestone(MillestoneDefinition millestoneDefiniton) {

        MillestoneModel millestoneModel = new MillestoneModel(assetManager);

        Material mat_ttOff = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_ttOff.setColor("Color", ColorRGBA.Blue);
        Material mat_ttOn = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_ttOn.setColor("Color", ColorRGBA.Red);

        millestoneModel.setMaterialOff(mat_ttOff);
        millestoneModel.setMaterialOn(mat_ttOn);

        return millestoneModel;
    }

    public ASimpleWater buildWater(Node rootNode, DirectionalLight lightDir, ViewPort viewPort) {
        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);

        SimpleWater simpleWater = new SimpleWater(rootNode, lightDir, fpp);
        
        viewPort.addProcessor(fpp);

        return simpleWater;
    }
    

    public GameModel buildGameModel(GameDefinition gameDef) {
        GameModel gameModel = new GameModel();

        //Ship
        for (java.util.Map.Entry<String, ShipDefinition> shipDef : gameDef.getShips().entrySet()) {
            ShipModel shipModel = buildShip(shipDef.getValue());
            //shipModel.setPosition(new Vector3f(shipDef.getValue().getPosition()[0], shipDef.getValue().getPosition()[1], shipDef.getValue().getPosition()[2]));
            gameModel.getShips().put(shipDef.getKey(), shipModel);
        }

        //Millestones
        for (java.util.Map.Entry<String, MillestoneDefinition> millestoneDef : gameDef.getMillestones().entrySet()) {
            MillestoneModel millestoneModel = buildMillestone(millestoneDef.getValue());
            millestoneModel.setLocalTranslation(new Vector3f(millestoneDef.getValue().getPosition()[0], millestoneDef.getValue().getPosition()[1], millestoneDef.getValue().getPosition()[2]));
            gameModel.getMillestones().put(millestoneDef.getKey(), millestoneModel);
        }

        //Sky
        if (gameDef.getSky() != null) {
            Spatial sky = SkyFactory.createSky(assetManager, gameDef.getSky().getSkyTexture(), gameDef.getSky().isSpheric());
            sky.setLocalScale(gameDef.getSky().getScale());
            gameModel.setSky(sky);
        }
        
        //Sun
        if(gameDef.getSun() !=null){
            SunModel sunModel = new SunModel(gameDef.getSun().getVector3f(), gameDef.getSun().getColorRGBA());
            gameModel.setSun(sunModel);
        }

        //Wind
        SimpleContext simpleContext = new SimpleContext();
        gameModel.setiContext(simpleContext);
        if(gameDef.getWind() !=null){
             simpleContext.setWind(new RotateSimpleWind(assetManager));
        }

        return gameModel;
    }
}
